﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using TireService.Data;

namespace TireService.Business
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            // Fetch customer id
            try
            {
                Guid userid = new Guid(Membership.GetUser(Login1.UserName).ProviderUserKey.ToString());

                TireServiceDB db = new TireServiceDB();

                int CustomerId = db.GetCustomerIdByUserId(userid);

                Customer customer = db.GetCustomer(CustomerId);

                if (!customer.IsCompany)
                {
                    // Customer not a company - logout user again
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/Login.aspx");
                }

                // Set session
                Session["CustomerId"] = CustomerId;
            }
            catch (Exception ex)
            {
                // Error occured - logout user again
                FormsAuthentication.SignOut();

                Response.Redirect("~/Login.aspx");
            }
        }
    }
}